from multiprocessing import Process, Lock


def lock_fun(lock, num):
    lock.acquire()
    try:
        print("hello world", num)
    finally:
        lock.release()


def nolock_fun(num):
    print("hello world", num)


if __name__ == "__main__":
    procs = []
    print("无锁场景----")
    for num in range(10):
        proc = Process(target=nolock_fun, args=(num,))
        proc.start()
        procs.append(proc)

    for proc in procs:
        proc.join()

    print("有锁场景++++")
    procs.clear()
    lock = Lock()
    for num in range(10):
        proc = Process(target=lock_fun, args=(lock, num))
        proc.start()
        procs.append(proc)

    for proc in procs:
        proc.join()
